package com.wpr.shop.context;


import com.wpr.shop.utils.ResultUtil;
import com.wpr.shop.modules.global.validate.ReturnCode;
import com.wpr.shop.modules.global.validate.ValidateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.invoke.MethodHandles;

@ControllerAdvice
class GlobalDefaultExceptionHandler {
   private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object defaultErrorHandler(HttpServletRequest req, Exception e){

        LOGGER.error("异常错误:", e);

        /**参数错误*/
        if(e instanceof ValidateException) {
            return ResultUtil.setResult(((ValidateException) e).getErrorCode());
        }
        return ResultUtil.setResult(ReturnCode.ERROR_GENER);
    }
}
